﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Lidgren.Network;
using System.Threading;
using Paypayes.Aelda.Core.SimulationEvents;
using Paypayes.Aelda.Core;

namespace Client
{
    static class Program
    {
        private static NetClient s_client;
        private static Form1 s_form;

        private static EventSerializer es = new EventSerializer();
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            s_form = new Form1();

            NetPeerConfiguration config = new NetPeerConfiguration("aelda");
            config.AutoFlushSendQueue = false;
            s_client = new NetClient(config);

            s_client.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));

            s_client.Start();
            NetOutgoingMessage hail = s_client.CreateMessage("This is the hail message");
            s_client.Connect("127.0.0.1", 21420, hail);

            Application.Run(s_form);

            s_client.Shutdown("Bye");
        }

        public static void GotMessage(object peer)
        {
            NetIncomingMessage im;
            while ((im = s_client.ReadMessage()) != null)
            {
                // handle incoming message
                switch (im.MessageType)
                {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        string text = im.ReadString();
                        Console.WriteLine(text);
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();

                        if (status == NetConnectionStatus.Connected)
                        {
                            Send("user", "password");
                        }

                        string reason = im.ReadString();
                        Console.WriteLine(status.ToString() + ": " + reason);

                        break;
                    case NetIncomingMessageType.Data:
                        string chat = im.ReadString();
                        
                        Console.WriteLine(chat);
                        break;
                    default:
                        Console.WriteLine("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes");
                        break;
                }
            }
        }

        // called by the UI
        public static void Send(string login, string password)
        {
            //Login lev = new Login()
            //{
            //    User = login,
            //    Password = password
            //};
            //lev.SourceId = 1;
            //lev.TargetId = 2;
            //Object om = s_client.CreateMessage();
            //es.Pack(lev, ref om);
            //s_client.SendMessage((NetOutgoingMessage)om, NetDeliveryMethod.ReliableOrdered);
            //Console.WriteLine("Sending '" +  login + "'");
            //s_client.FlushSendQueue();
        }
    }
}